import 'package:flutter/material.dart';
import 'package:ost_flutter_module/common/adapt.dart';
import 'package:ost_flutter_module/common/color.dart';

class GImageTextButton extends StatelessWidget {
  /// 优先加载 image -> svg -> icon
  final ImageTextStyle style;
  final String image;
  final IconData icon;
  final Widget svg;
  final Color iconColor;
  final double iconSize;
  final String text;
  final TextStyle textStyle;
  final Color backgroundColor;
  final double height;

  /// image and text padding
  final double textLeadingPadding;
  final double padding;
  final double borderRadius;
  final MainAxisSize mainAxisSize;
  final ValueChanged<String> onTap;

  const GImageTextButton(
    this.text, {
    Key key,
    this.image,
    this.icon,
    this.iconColor,
    this.iconSize,
    this.textStyle,
    this.backgroundColor = Colors.white,
    this.textLeadingPadding = 8,
    this.padding = 0,
    this.borderRadius = 5,
    this.mainAxisSize = MainAxisSize.max,
    this.onTap,
    this.style = ImageTextStyle.imageLeft,
    this.height = 36,
    this.svg,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Material(
      color: backgroundColor,
      borderRadius: BorderRadius.circular(Adapt.px(borderRadius)),
      child: Ink(
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(Adapt.px(borderRadius)),
        ),
        child: InkWell(
          borderRadius: BorderRadius.circular(Adapt.px(borderRadius)),
          onTap: () => onTap(text),
          child: Padding(
            padding: EdgeInsets.symmetric(horizontal: Adapt.px(padding)),
            child: SizedBox(
              height: Adapt.px(height),
              child: _build(style),
            ),
          ),
        ),
      ),
    );
  }

  Widget _build(ImageTextStyle style) {
    if (style == ImageTextStyle.imageLeft || style == ImageTextStyle.imageRight)
      return _buildRow(style);
    return _buildColumn();
  }

  Widget _buildRow(ImageTextStyle style) => Row(
        mainAxisAlignment: MainAxisAlignment.center,
        mainAxisSize: mainAxisSize,
        children: style == ImageTextStyle.imageLeft
            ? [
                _buildImage(),
                SizedBox(width: Adapt.px(textLeadingPadding)),
                _buildText(),
              ]
            : [
                _buildText(),
                SizedBox(width: Adapt.px(textLeadingPadding)),
                _buildImage(),
              ],
      );

  Widget _buildColumn() => Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: style == ImageTextStyle.imageTop
            ? [
                _buildImage(),
                SizedBox(width: Adapt.px(textLeadingPadding)),
                _buildText(),
              ]
            : [
                _buildText(),
                SizedBox(width: Adapt.px(textLeadingPadding)),
                _buildImage(),
              ],
      );

  Widget _buildImage() {
    return image != null && image.isNotEmpty
        ? AssetImage(image)
        : svg != null
            ? svg
            : icon != null
                ? Icon(icon, color: iconColor ?? color999999, size: iconSize)
                : Container();
  }

  Widget _buildText() {
    return Text(
      text ?? '',
      style: textStyle ?? TextStyle(color: color666666, fontSize: 14),
    );
  }
}

/// 图文并排方式
enum ImageTextStyle { imageLeft, imageTop, imageRight, imageBottom }
